package com.woniuxy.service.hospital.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Dept;
import com.woniuxy.entity.Hospital;
import com.woniuxy.mapper.HospitalMapper;
import com.woniuxy.mapper.custom.CustomDeptMapper;
import com.woniuxy.service.hospital.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class DeptServiceImpl implements DeptService {


    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private CustomDeptMapper deptMapper;

    @Override
    public PageInfo<Dept> findAll(Integer page,Integer size) {
        PageHelper.startPage(page, size);
        List<Dept> list = deptMapper.selectByExample(null);
        for (Dept dept : list) {
            dept.sethName(deptMapper.findHnameByHid(dept.gethId()));
        }
        PageInfo info = new PageInfo(list);
        return info;
    }

    @Override
    public int save(Dept dept) {
         return deptMapper.insertSelective(dept);
    }



    @Override
    public int update(Dept dept) {
        int rows = deptMapper.updateByPrimaryKeySelective(dept);
        return rows;
    }

    @Override
    public List<Dept> findAllDept(Integer hid) {
        List<Dept> allDept = deptMapper.findAllDept(hid);
        for (Dept dept : allDept) {
            Hospital hospital = hospitalMapper.selectByPrimaryKey(hid);
            dept.sethName(hospital.gethName());
        }
        return allDept;
    }

    @Override
    public int delete(Integer deptId) {
        int row = deptMapper.deleteByPrimaryKey(deptId);
        return row;
    }

    @Override
    public List<Dept> findLike(String hName) {
        String like="%"+hName+"%";
        List<Hospital> list = hospitalMapper.findLike(like);
        List<Dept> deptList =new ArrayList<>();
        for (Hospital hospital : list) {
             List<Dept> allDept = deptMapper.findAllDept(hospital.gethId());
            for (Dept dept : allDept) {
                dept.sethName(hospital.gethName());
                deptList.add(dept);
            }
        }
        return deptList;
    }
}
